package cn.xiangxiang.service.page;

import cn.xiangxiang.pojo.goods.Goods;
import cn.xiangxiang.pojo.goods.Sku;
import cn.xiangxiang.pojo.goods.Spu;
import cn.xiangxiang.service.goods.CategoryService;
import cn.xiangxiang.service.goods.SpuService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageConsumer implements MessageListener {

    @Reference
    private SpuService spuService;
    @Reference
    private CategoryService categoryService;
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void onMessage(Message message) {
        String spuid = new String(message.getBody());

        Goods goods = spuService.findsgoodsbyid(spuid);

        Spu spu = goods.getSpu();

        List<Sku> skuList = goods.getSkuList();

        HashMap urlMap = new HashMap();
        for (Sku sku : skuList) {
            if("1".equals(sku.getStatus())) {
                String specJSonString = JSON.toJSONString(JSON.parse(sku.getSpec()), SerializerFeature.MapSortField);
                urlMap.put(specJSonString, sku.getId() + ".html");
            }
        }

        for (Sku sku : skuList) {
            Context context = new Context();
            HashMap dataMap = new HashMap();
            String skuName = sku.getName();
            dataMap.put("skuName",skuName);
            dataMap.put("caption",spu.getCaption());

            dataMap.put("price",sku.getPrice());

            dataMap.put("introduction",spu.getIntroduction());

            Map specMap = JSON.parseObject(sku.getSpec());
            dataMap.put("spec",specMap);

            String paraItems = spu.getParaItems();
            if(paraItems == null){
                paraItems = "{}";
            }
            Map  parmItemMap  = JSON.parseObject(paraItems);
            dataMap.put("paraItems",parmItemMap);


            String images = spu.getImages();
            if(images == null){
                images = "[]";
            }
            List imageList = (List)JSON.parseArray(images);
            ArrayList<String> spuImgList = new ArrayList<>();
            for (Object obj : imageList) {
                Map map = (Map)JSON.parseObject(obj.toString());
                Object url = map.get("url");
                spuImgList.add(url.toString());
            }

            dataMap.put("spuImgList",spuImgList);

            String skuImages = sku.getImages();
            if(skuImages == null){
                skuImages = "[]";
            }
            List skuImagesList = (List)JSON.parseArray(skuImages);
            ArrayList<String> skuImgList = new ArrayList<>();
            for (Object obj : skuImagesList) {
                Map map = (Map)JSON.parseObject(obj.toString());
                Object url = map.get("url");
                skuImgList.add(url.toString());
            }
            dataMap.put("skuImgList",skuImgList);

            dataMap.put("spuIamge",spu.getImage());


            ArrayList<String> categoryNames = new ArrayList<>();
            categoryNames.add(categoryService.getById(spu.getCategory1Id()).getName());
            categoryNames.add(categoryService.getById(spu.getCategory2Id()).getName());
            categoryNames.add(categoryService.getById(spu.getCategory3Id()).getName());
            categoryNames.add(spu.getName());

            dataMap.put("categoryNames",categoryNames);

            String specItems = spu.getSpecItems();
            if(specItems==null){
                specItems="{}";
            }
            Map mapSpecItems = (Map) JSON.parse(specItems);


            String spec = sku.getSpec();
            Map skuSpecMap =  (Map) JSON.parse(spec);

//            {"颜色":["红色","蓝色","黑色","金色"],"版本":["6GB+128GB","6GB+64GB"]}
//            {"颜色":[{"options":"红色","checked:true},{"options":"蓝色","checked:false}]}
            HashMap<Object, Object> map = new HashMap<>();
            for(Object key : mapSpecItems.keySet()){
                Object value = mapSpecItems.get(key);
                List<String> specValueList =  (List<String>)(value);
                ArrayList arrayList = new ArrayList();
                for(String v:specValueList){
                    HashMap<String, Object> newspecMap = new HashMap<>();
                    if(skuSpecMap.get(key).equals(v)){
                        newspecMap.put("options",v);
                        newspecMap.put("checked",true);
                    }else{
                        newspecMap.put("options",v);
                        newspecMap.put("checked",false);
                    }


                    /*排序，中带你*/
                    Map specJsonMap = (Map) JSON.parse(sku.getSpec());
                    specJsonMap.put(key,v);
                    String specJson = JSON.toJSONString(specJsonMap, SerializerFeature.MapSortField);// 排序
                    newspecMap.put("url",urlMap.get(specJson));


                    arrayList.add(newspecMap);
                }
                mapSpecItems.put(key,arrayList);
            }


//            mapSpecItems = map;
            dataMap.put("mapSpecItems" ,mapSpecItems);

//            skuid
            dataMap.put("skuId",sku.getId());

            context.setVariables(dataMap);

            File file = new File("d:\\genhtml");
            if(!file.exists()){
                file.mkdirs();
            }

            String htmlName = sku.getId() +".html";
            File fileName = new File(file, htmlName);

            try {

                FileWriter writer = new FileWriter(fileName);

                templateEngine.process("item",context,writer);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
